#!/usr/bin/env python  
# -*- coding: utf-8 -*-  
from HTMLParser import HTMLParser
import sys,re,urllib2,urllib,time,os,json,codecs,csv

type = sys.getfilesystemencoding() 
reload(sys)  
sys.setdefaultencoding('utf8')

class Test:
    def __init__(self):
        self.page = 1
        self.date_items = []
        self.section_one = 16
        self.section_two = 16
        self.overflow_exist = 1
        self.user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
        self.headers    = { 'User-Agent' : self.user_agent }
        self.tab = '汽車新聞'

    def start(self):  
        # url         = "https://autos.yahoo.com.tw/news/audi-a5-sportback-%E8%88%89%E5%A5%AA%E4%B8%8B2018%E8%BB%8A%E8%A8%8A%E9%A2%A8%E9%9B%B2%E7%8D%8E-%E6%9C%80%E4%BD%B3%E9%80%B2%E5%8F%A3%E4%B8%AD%E5%A4%A7%E5%9E%8B%E8%BB%8A-031200727.html"
        url = 'https://autos.yahoo.com.tw/news/%E6%AD%A1%E6%85%B6%E4%B8%89%E5%91%A8%E5%B9%B4-honda-motorcycle%E5%84%AA%E6%83%A0%E6%B4%BB%E5%8B%95%E9%96%8B%E8%B7%91-054735169.html'
        request     = urllib2.Request(url, headers = self.headers)
        response    = urllib2.urlopen(request).read()
        page_html   =  response.decode("UTF-8").encode(type)
        self.fnCreateCsv(page_html)
        
    def fnCreateCsv(self,page_html):    
        
        title = re.findall('<h1 class="title">(.*?)</h1>',page_html)
        source = re.findall('<div class="detail">\n.*?<span>(.*?)</span>',page_html)
        dateOrauthor = re.findall('<div class="detail">\n.*?<span>.*?</span>\n.*?<span>(.*?)</span>',page_html)

        print len(dateOrauthor[0])

        if(len(dateOrauthor) != 19):
            split_d_a = dateOrauthor[0].split(" ",1)
            items = [split_d_a[0],split_d_a[1],title[0],source[0]]
        else:
            items = ['',dateOrauthor[0],title[0],source[0]]

        self.date_items.append(items)


spider = Test()
spider.start()
